// index.js
// 获取应用实例
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    motto: '随身听',
    avatarUrl: defaultAvatarUrl,
    login:false,
    nickname:'',
    userInfo:{},
    openid:'',
    hasUserInfo: false,//本地缓存中是否有login_key,保存着用户信息
    canIUseGetUserProfile: false,
    canIUseOpenData: false // 如需尝试获取用户信息可改为false
    // wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') 
  },
  // 事件处理函数
  bindViewTap() {
    console.log(this.data.login)
    if(this.data.login==true){
      wx.redirectTo({
        url: '../home/home'
      })
    }else{
      wx.showToast({
        title: '请先进行登录',
        duration:1500,
        success:function(){
          return ;
        }
      })
    }
  },
  onLoad() {
    if (wx.getUserProfile) {
      // app.globaldata.login_key=wx.getStorage('login_key')
      this.setData({
        canIUseGetUserProfile: true,
        nickname:this.data.userInfo.nickName,
        // login_key:app.globaldata.login_key,
        avatarUrl:this.data.avatarUrl,
        login:true
      })
    }
    if(wx.getStorageSync('login_key')){//如果已经缓存用户信息就直接进入
      this.setData({
        hasUserInfo:true,
        userInfo:wx.getStorageSync('login_key'),
      })
    }
  },
  onShow: function () {
  },
  //修改用户头像
  chooseavatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
    console.log(this.data.avatarUrl)
    console.log(this.data.userInfo)
    wx.uploadFile({
      //请求后台的路径
      url: 'https://www.walkman.asia:2023/change/photo',
      //小程序本地的路径
      filePath: this.data.avatarUrl,
      //后台获取我们图片的key
      name: 'image',
      formData:{
        photo:this.data.userInfo.user_name+".jpg",
        id:this.data.userInfo.user_id
      },
      success: function (res) {
        console.log(res)
        if(res.statusCode==200){
          this.data.userInfo.user_head = this.data.userInfo.user_name+".jpg";
          wx.setStorageSync('login_key', this.data.userInfo)
          this.setData({
            userInfo: wx.getStorageSync('login_key')
        })
        }
      },
    })
  },
  changeName: function (e) {
    this.setData({
      nickname:e.detail.value,
    })
    this.data.userInfo.user_name = this.data.nickname;
    wx.setStorageSync('login_key', this.data.userInfo)
    console.log(this.data.nickname)
    console.log(this.data.userInfo)
    wx.request({
      url: 'https://www.walkman.asia:2023/change/name',
      method:'post',
      data:{
        name:this.data.nickname,
        id:this.data.userInfo.user_id
      },
      success:function(res){
        console.log(res)
      },
    })
  },
  //当缓存中没有用户信息时 才会调用此方法缓存用户信息
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          //发起网络请求
          wx.request({
            url: `https://www.walkman.asia:2023/login/${res.code}`,
            success: function (res) {
              wx.setStorageSync("login_key", res.data[0])
              // app.globaldata.login_key=res.data
              // console.log(app.globaldata.login_key)
              that.setData({
                hasUserInfo:true,
                userInfo:res.data[0],
              })
              console.log(that.data.userInfo)
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
          debugger
        }
      }
    })
  },
})